package com.markerhub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.ResponseJson;
import com.markerhub.entity.Blog;
import com.markerhub.mapper.BlogMapper;
import com.markerhub.service.BlogService;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-04-10
 */
@Api(value = "UserController",tags = "博客管理接口")
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    BlogService blogService;

    @ApiOperation(value = "获取最新博客前七条数据")
    @GetMapping("/LatesList")
    public Object getLatesList(){
        Page page = new Page(1, 7);
        IPage pageData = blogService.page(page, new QueryWrapper<Blog>().orderByDesc("create_time"));
        try{
            return ResponseJson.success(pageData);
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "获取博客分类数据")
    @GetMapping("/TypeList")
    public Object getTypeList(){
        try{
            return ResponseJson.success(blogService.getTpyelist());
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "获取博客标签数据")
    @GetMapping("/TagList")
    public Object getTagList(){
        try{
            return ResponseJson.success(blogService.getTaglist());
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询博客数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页号",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,dataType = "int"),
            @ApiImplicitParam(name = "column",value = "排序字段",required = true,dataType = "string"),
            @ApiImplicitParam(name = "sort",value = "升序0降序1",required = true,dataType = "int"),
            @ApiImplicitParam(name = "querycontent",value = "搜索字段",required = false,dataType = "string"),
    })
    @GetMapping("/BlogList")
    public Object getBlogList(Integer current, Integer size, String column, Integer sort, String querycontent){
        IPage<Blog> blogs=blogService.selectBlogs(current,size,column,sort,querycontent);
        try{
            return ResponseJson.success(blogs);
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "根据id查询博客")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "博客id",required = true,dataType = "int")
    })
    @GetMapping("/BlogListById")
    public Object getBlogById(Integer id){
        Blog blog=blogService.getById(id);
        try{
            return ResponseJson.success(blog);
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "根据分类查询博客数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页号",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,dataType = "int"),
            @ApiImplicitParam(name = "column",value = "排序字段",required = true,dataType = "string"),
            @ApiImplicitParam(name = "sort",value = "升序0降序1",required = true,dataType = "int"),
            @ApiImplicitParam(name = "type_id",value = "类别id",required = true,dataType = "int"),
    })
    @GetMapping("/BlogListByType")
    public Object getBlogListByType(Integer current, Integer size, String column, Integer sort, Integer type_id){
        IPage<Blog> blogs=blogService.selectBlogsbyType(current,size,column,sort,type_id);
        try{
            return ResponseJson.success(blogs);
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "根据标签查询博客数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页号",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,dataType = "int"),
            @ApiImplicitParam(name = "column",value = "排序字段",required = true,dataType = "string"),
            @ApiImplicitParam(name = "sort",value = "升序0降序1",required = true,dataType = "int"),
            @ApiImplicitParam(name = "tag_id",value = "标签id",required = true,dataType = "int"),
    })
    @GetMapping("/BlogListByTag")
    public Object getBlogListByTag(Integer current, Integer size, String column, Integer sort, Integer tag_id){
        IPage<Blog> blogs=blogService.selectBlogsbyTag(current,size,column,sort,tag_id);
        try{
            return ResponseJson.success(blogs);
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "添加博客")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "标题",required = true,dataType = "string"),
            @ApiImplicitParam(name = "uid",value = "用户id",required = true,dataType = "int"),
            @ApiImplicitParam(name = "content",value = "博客内容",required = true,dataType = "string"),
            @ApiImplicitParam(name = "firstPicture",value = "图片链接",required = true,dataType = "string"),
            @ApiImplicitParam(name = "flag",value = "完成状态",required = true,dataType = "string"),
            @ApiImplicitParam(name = "recommend",value = "是否推荐",required = true,dataType = "boolean"),
            @ApiImplicitParam(name = "appreciation",value = "是否开启赞赏",required = true,dataType = "boolean"),
            @ApiImplicitParam(name = "commentabled",value = "是否开启评论",required = true,dataType = "boolean"),
            @ApiImplicitParam(name = "tagId",value = "标签id",required = true,dataType = "int"),
            @ApiImplicitParam(name = "typeId",value = "类型id",required = true,dataType = "int"),
            @ApiImplicitParam(name = "shareStatement",value = "版权状态",required = true,dataType = "string"),
            @ApiImplicitParam(name = "description",value = "描述",required = true,dataType = "string"),
    })
    @PostMapping("/addBlog")
    public Object addBlog(String title, Integer uid,String content,String firstPicture,String flag,Boolean recommend,Boolean appreciation,Boolean commentabled,Integer tagId,Integer typeId,String shareStatement,String description){
        try{
            blogService.addBlog(title,uid,content,firstPicture,flag,recommend,appreciation,commentabled,tagId,typeId,shareStatement,description);
            return ResponseJson.success();
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "文件上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "上传文件",required = true,dataType = "__file",paramType = "form"),
    })
    @PostMapping(value="file_upload",consumes = "multipart/*", headers="content-type=multipart/form-data")
    public Object  fileUpload(HttpServletRequest request, @ApiParam(value="上传", required = true) MultipartFile file) {
        try {
            String filename=file.getOriginalFilename();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
            Date date=new Date();
            String str=sdf.format(date);
            String filepath="/Users/caomao/Downloads/A/前端/blog-vue/src/assets/images/blog_index/";
            String newfielename=str+filename;
            File targetFile = new File(filepath,newfielename);
            file.transferTo(targetFile);
            String res=newfielename;
            return ResponseJson.success(res);
        } catch (Exception e) {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "博客修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "blog",value = "blog类",required = true,dataType = "Blog"),
    })
    @PostMapping(value="change_blog")
    public Object  changeBlog(Blog blog) {
        try {
            blogService.changeblog(blog);
            return ResponseJson.success();
        } catch (Exception e) {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "博客删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "blogid",value = "博客id",required = true,dataType = "int"),
    })
    @DeleteMapping(value="delete_blog")
    public Object  deleteBlog(Integer blogid) {
        try {
            blogService.deleteBlog(blogid);
            return ResponseJson.success();
        } catch (Exception e) {
            return ResponseJson.failure(e.getMessage());
        }
    }

}
